package com.sen.controller;


import com.sen.dto.RoleResourceDTO;
import com.sen.entity.Role;
import com.sen.result.Result;
import com.sen.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sen
 * @since 2021-03-29
 */
@Api(tags = "角色控制器")
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @ApiOperation(value = "获取角色列表，包括角色拥有的资源信息")
    @GetMapping("/list/{cur}/{size}")
    public Result getRoleList(@PathVariable Integer cur, @PathVariable Integer size){
        Result result = roleService.getRoleList(cur, size);
        return result;
    }

    @ApiOperation(value = "删除一个角色，以及会删除该角色在角色资源表中的记录")
    @DeleteMapping("/{id}")
    public Result deleteRoleById(@PathVariable Integer id){
        Result result = roleService.deleteRoleById(id);
        return result;
    }

    @ApiOperation(value = "新增一个角色")
    @PostMapping
    public Result addRole(@RequestBody Role role){
        Result result = roleService.saveRole(role);
        return result;
    }

    @ApiOperation(value = "更新角色信息")
    @PutMapping("/updateRole")
    public Result updateRole(@RequestBody Role role){
        Result result = roleService.updateRole(role);
        return result;
    }

    @ApiOperation(value = "更新角色的资源信息")
    @PutMapping("/updateResource")
    public Result updateResource(@RequestBody RoleResourceDTO roleResourceDTO){
        System.out.println(roleResourceDTO);
        Result result = roleService.updateRoleResource(roleResourceDTO.getRoleId(), roleResourceDTO.getResourceIdList());
        return result;
    }

}
